package cn.iocoder.yudao.module.member.point.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.exception.Assertions;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.api.member.point.enums.MemberPointBizTypeEnum;
import cn.iocoder.yudao.module.member.assets.dal.dataobject.MemberDigitalAssetsDO;
import cn.iocoder.yudao.module.member.assets.service.AppMemberDigitalAssetsService;
import cn.iocoder.yudao.module.member.point.dal.dataobject.MemberPointRecordDO;
import cn.iocoder.yudao.module.member.point.dal.mysql.MemberPointRecordMapper;
import cn.iocoder.yudao.module.member.point.vo.AppMemberPointRecordPageReqVO;
import cn.iocoder.yudao.module.member.point.vo.MemberPointRecordPageReqVO;
import cn.iocoder.yudao.module.member.user.dal.dataobject.MemberUserDO;
import cn.iocoder.yudao.module.member.user.service.MemberUserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;


/**
 * 积分记录 Service 实现类
 *
 * @author QingX
 */
@Slf4j
@Service
@Validated
public class MemberPointRecordServiceImpl implements MemberPointRecordService {

    @Resource
    private MemberPointRecordMapper memberPointRecordMapper;

    @Resource
    private MemberUserService memberUserService;
    @Resource
    private AppMemberDigitalAssetsService memberDigitalAssetsService;

    @Override
    public PageResult<MemberPointRecordDO> getPointRecordPage(MemberPointRecordPageReqVO pageReqVO) {
        // 根据用户昵称查询出用户 ids
        Set<Long> userIds = null;
        if (StringUtils.isNotBlank(pageReqVO.getNickname())) {
            List<MemberUserDO> users = memberUserService.getUserListByNickname(pageReqVO.getNickname());
            // 如果查询用户结果为空直接返回无需继续查询
            if (CollectionUtils.isEmpty(users)) {
                return PageResult.empty();
            }
            userIds = convertSet(users, MemberUserDO::getId);
        }
        // 执行查询
        return memberPointRecordMapper.selectPage(pageReqVO, userIds);
    }

    @Override
    public PageResult<MemberPointRecordDO> getPointRecordPage(Long userId, AppMemberPointRecordPageReqVO pageReqVO) {
        return memberPointRecordMapper.selectPage(userId, pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createPointRecord(Long userId, Integer point, MemberPointBizTypeEnum bizType, String bizId) {
        // 1. 校验用户积分
        MemberDigitalAssetsDO digitalAssets = memberDigitalAssetsService.getOrCreate(userId);
        int userPoint = ObjectUtil.defaultIfNull(digitalAssets.getPoint(), 0);
        int totalPoint = userPoint + point; // 用户变动后的积分
        Assertions.isTrue(totalPoint >= 0, "积分余额不足");
        // 2. 更新用户积分
        memberDigitalAssetsService.changePoint(userId, point);
        // 3. 增加积分记录
        MemberPointRecordDO record = new MemberPointRecordDO();
        record.setUserId(userId);
        record.setBizId(bizId);
        record.setBizType(bizType.getType());
        record.setTitle(bizType.getName());
        record.setDescription(StrUtil.format(bizType.getDescription(), point));
        record.setPoint(point);
        record.setTotalPoint(totalPoint);
        memberPointRecordMapper.insert(record);
    }

}
